﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：查看模型计算结果
 */

var keyValue = request('modelId');
var defaultCompanyId = request('companyId');
var defaultKjnd = request('defaultKjnd');
var warnId = request('warnId');

var userdefault = {};
var selectedRow;
var step2List = [];

var bootstrap = function ($, learun) {
    "use strict";

    var jfGrid1 = null;
    var jfGrid2 = null;
    var params = [];

    // 保存数据
    var acceptClick = function (type) {// 0保存并新增 1保存

    };

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            if(!!defaultCompanyId) {
                userdefault.companyId = defaultCompanyId;
            }
            page.bind();
            page.initData();
        },
        bind: function () {
            // 优化滚动条
            $('.lr-layout-wrap').lrscroll();
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                // 调用后台查询
                // queryJson 查询条件
                page.search(queryJson);

            }, 200, 888);

            // 公司选择
            $('#companyId').lrselect({
                type: 'tree',
                placeholder: '==全部单位==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
            });
            $('#companyId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);

            // 科目余额情况表
            jfGrid1 = $('#kjkmGrid').jfGrid({
                headData: [
                    {label: '单位名称', name: 'companyName', width: 160, align: 'left'},
                    {label: '科目名称', name: 'kjkmCodeAndName', width: 260, align: 'left'},
                    {label: "会计年度", name: "kjnd", width: 80, align: "center"},
                    {
                        label: '余额', name: 'ye', width: 180, align: 'right', statistics: true,
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '机构id', name: 'companyId', width: 1, align: 'right', hidden: true}
                ],
                dblclick: function (row) {
                    var companyId = $('#kjkmGrid').jfGridValue('companyId');
                    var kjnd = $('#kjkmGrid').jfGridValue('kjnd');
                    var kjkmCode = $('#kjkmGrid').jfGridValue('kjkmCode');

                    var step2List_temp = [];
                    for (let i = 0; i < step2List.length; i++) {
                        if (step2List[i].companyId == companyId
                            && step2List[i].kjnd == kjnd
                            && step2List[i].kjkmCode == kjkmCode) {
                            step2List_temp.push(step2List[i]);
                        }
                    }
                    $('#xmGrid').jfGridSet('refreshdata', step2List_temp);
                },
                height: 200
            });
            // 项目明细情况表
            jfGrid2 = $('#xmGrid').jfGrid({
                headData: [
                    {label: '科目名称', name: 'kjkmCodeAndName', width: 260, align: 'left'},
                    {label: "会计年度", name: "kjnd", width: 80, align: "center"},
                    {label: "项目名称", name: "ysxmCodeAndName", width: 260, align: "left"},
                    {
                        label: '余额', name: 'qm', width: 180, align: 'right', statistics: true,
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '机构id', name: 'companyId', width: 1, align: 'right', hidden: true}
                ],
                height: 380
            });

            // 整改
            $('#btn_deal').on('click', function () {
                learun.layerForm({
                    id: 'warn_deal' + keyValue,
                    title: '告警整改',
                    url: config.webportal_server + '/pages/workflow/wfmyTask/custmerWorkFlowForm.html?keyValue=' + keyValue + '&modelId=' + keyValue + '&warnId=' + warnId + '&shcemeCode=warn_deal&tabIframeId=5456a5f791844dbaaa1474af2a70e8db&type=0',
                    width: 1000,
                    height: 800,
                    maxmin: true,
                    btn: null
                });
            });
            // 忽略
            $('#btn_ingore').on('click', function () {
                learun.layerForm({
                    id: 'warn_ingore' + keyValue,
                    title: '告警忽略',
                    url: config.webportal_server + 'pages/finance/audit_warn/ignore/form.html?warnId=' + warnId + '&access_token=' + config.getToken().access_token,
                    width: 450,
                    height: 280,
                    callBack: function (id) {
                        return top[id].acceptClick();
                    }
                });
            });

            //下载
            $('#btn_download').on('click', function () {
               var companyId = $("#companyId").lrselectGet();
               var kjnd = $("#kjnd").val();
               location.href = config.base_server() + 'auditmodels/exportExcel?modelId=' + keyValue + '&companyId=' + companyId + '&kjnd=' + kjnd + '&warnId=' + warnId + '&access_token=' + config.getToken().access_token;
            });

        },
        initData: function () {
            if (!!keyValue) {
               page.search();
            }
            if (!!warnId) {
                $.ajax({
                    url: config.base_server() + 'auditmodelwarns/updateReadFlag?id=' + warnId + '&access_token=' + config.getToken().access_token,
                    type: "GET",
                    success: function (data) {
                    }
                });
            }
        },
        search: function (param) {
            $('#lr_form_bg').fadeIn();
            param = param || {};
            var companyId = $("#companyId").lrselectGet();
            if(!companyId && !!defaultCompanyId) {
                companyId = defaultCompanyId;
            }
            var xmName = $("#xmName").val();
            param.companyId = companyId;
            param.xmName = xmName;

            page.refreshData(param);
        },
        refreshData: function (param) {
            learun.httpAsync('GET', config.base_server() + 'auditmodels/previewResult?modelId=' + keyValue  + '&warnId=' + warnId + '&access_token=' + config.getToken().access_token, param, function (data) {
                $('#lr_form_bg').fadeOut();
                $('.lr-layout-wrap').lrSetFormData(data);
                $("#modelName").html(data.modelName);
                $('#kjkmGrid').jfGridSet('refreshdata', data.step1List);

                step2List = data.step2List;
                if(step2List==null) {
                    step2List = [];
                }
                $('#xmGrid').jfGridSet('refreshdata', step2List);
            });
        }
    };

    page.init();
}
